package com.taxbyb.open.util;

import cn.hutool.core.util.StrUtil;
import org.nutz.lang.Lang;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class SignUtil {

    /**
     * 对字段进行排序，然后使用=和&进行连接
     */
    public static String sortAndJoinField(Object obj) {
        return sortAndJoinField(obj, "sign");
    }

    public static String sortAndJoinField(Object obj, String excludeField) {
        Map<String, Object> map = Lang.obj2map(obj);
        List<String> sortedKeyList = new ArrayList<>(map.keySet());
        Collections.sort(sortedKeyList);

        StringBuilder builder = new StringBuilder();
        for (String key : sortedKeyList) {
            if (excludeField.equals(key)) {
                continue;
            }

            builder.append(key)
                    .append("=")
                    .append(map.get(key))
                    .append("&");
        }

        return StrUtil.subPre(builder.toString(), -1);
    }
}
